﻿Imports System.Data.SqlClient
Imports RunEnv

Public Class CField
    Public Enum FieldRelation As Integer
        RelationCommon = 0
        RelationDependent = 1
        RelationValueRange = 2
        RelationTranslate = 3
    End Enum

    Public Property FieldName As String
    Public Property FieldTitle As String
    Public Property DataType As String
    Public Property NumericScale As Integer
    Public Property DataSize As Integer
    Public Property DefaultValue As String
    Public Property IsUnique As Boolean
    Public Property IsReadOnly As Boolean
    Public Property IsCanBeNull As Boolean
    Public Property IsVisible As Boolean
    Public Property IsDBField As Boolean

    Public Property FieldMode As FieldRelation
    Public Property SourceTable As String
    Public Property SourceFieldValue As String
    Public Property SourceFieldKey As String
    Public Property LocalFieldKey As String

    Private tableInfo As CTableInfo
    Private observers As New ArrayList

    Public Sub RegisterObserver(ByRef observer As CField)
        observers.Add(observer)
    End Sub

    Public Sub notifyObservers(ByVal value As Object, ByVal field As CField)
        For Each objField As CField In observers
            objField.Update(value, field)
        Next
    End Sub

    Private Sub Update(ByVal value As Object, ByVal field As CField)
        Dim command As New SqlCommand
        Dim sqlDataAdapter As New SqlDataAdapter
        Dim dt1 As New DataTable
        Try
            command.CommandText = "Select " & SourceFieldValue _
                & " From " & SourceTable _
                & " Where " & SourceFieldKey & "= @Value"
            command.Parameters.Add("@Value", CDBConnection.GetSqlDbType(field.DataType), field.DataSize).Value = value
            command.Connection = CRunEnv.DBConnection.sqlConnection
            sqlDataAdapter.SelectCommand = command
            sqlDataAdapter.Fill(dt1)
            tableInfo.SetValue(FieldName, dt1.Rows(0).Item(SourceFieldValue))
        Catch ex As Exception
            tableInfo.SetValue(FieldName, DBNull.Value)
        End Try
    End Sub

    Public Sub New(ByRef dtRow As DataRow, ByRef tableInfo As CTableInfo)
        Me.tableInfo = tableInfo
        FieldName = IIf(IsDBNull(dtRow.Item("FieldName")), "", _
                dtRow.Item("FieldName"))
        FieldTitle = IIf(IsDBNull(dtRow.Item("FieldTitle")), "", _
                dtRow.Item("FieldTitle"))
        DataType = IIf(IsDBNull(dtRow.Item("DataType")), "", _
                dtRow.Item("DataType"))
        NumericScale = IIf(IsDBNull(dtRow.Item("NumericScale")), 0, _
                dtRow.Item("NumericScale"))
        DataSize = IIf(IsDBNull(dtRow.Item("DataSize")), 0, _
                dtRow.Item("DataSize"))
        DefaultValue = IIf(IsDBNull(dtRow.Item("DefaultValue")), "", _
                dtRow.Item("DefaultValue"))
        IsUnique = IIf(IsDBNull(dtRow.Item("IsUnique")), False, _
                dtRow.Item("IsUnique"))
        IsReadOnly = IIf(IsDBNull(dtRow.Item("IsReadOnly")), True, _
                dtRow.Item("IsReadOnly"))
        IsCanBeNull = IIf(IsDBNull(dtRow.Item("IsCanBeNull")), True, _
                dtRow.Item("IsCanBeNull"))
        IsVisible = IIf(IsDBNull(dtRow.Item("IsVisible")), True, _
                dtRow.Item("IsVisible"))
        IsDBField = IIf(IsDBNull(dtRow.Item("IsDBField")), False, _
                dtRow.Item("IsDBField"))
        FieldMode = IIf(IsDBNull(dtRow.Item("FieldMode")), 0, _
                dtRow.Item("FieldMode"))
        SourceTable = IIf(IsDBNull(dtRow.Item("SourceTable")), "", _
                dtRow.Item("SourceTable"))
        SourceFieldValue = IIf(IsDBNull(dtRow.Item("SourceFieldValue")), "", _
                dtRow.Item("SourceFieldValue"))
        SourceFieldKey = IIf(IsDBNull(dtRow.Item("SourceFieldKey")), "", _
                dtRow.Item("SourceFieldKey"))
        LocalFieldKey = IIf(IsDBNull(dtRow.Item("LocalFieldKey")), "", _
                dtRow.Item("LocalFieldKey"))
    End Sub


    Public Function GetDefaultValue() As Object
        If Len(DefaultValue) <= 0 Then Return DBNull.Value

        If Left(DefaultValue, 1) <> "=" Then
            If CDBConnection.SqlType2Type(DataType) = "".GetType Then
                Return DefaultValue
            ElseIf CDBConnection.SqlType2Type(DataType) = (New Boolean()).GetType Then
                If DefaultValue.ToLower = "true" Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return DefaultValue
            End If
        Else
            Select Case Mid(DefaultValue, 2).ToLower
                Case "year()"
                    Return Year(Date.Now)
                Case "date()"
                    Return Date.Now
                Case "currentuser()"
                    Return CRunEnv.CurrentUser.UserName
                Case Else
                    Return DBNull.Value
            End Select
        End If
    End Function

End Class
